package com.qding.park.adapter.bsg.adapter.impl;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qding.park.adapter.bsg.adapter.config.BSGConfig;
import com.qding.park.adapter.bsg.adapter.constant.ConstantFunction;
import com.qding.park.adapter.bsg.adapter.entity.ResponseMsg;
import com.qding.park.adapter.bsg.adapter.enums.ErrorCodeEnum;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.network.HttpUtil;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.target.common.AdapterParam;
import com.qding.park.target.common.adapter.LockCarAdapter;
import com.qding.park.target.common.entity.AdLockCarOrder;

/**
 * @ClassName LockCarAdapterImpl
 * @Description TODO(锁车适配实现)
 * @author Administrator
 * @Date 2016年8月28日 下午9:46:30
 * @version 1.0.0
 */
@Service("lockCarAdapter_12")
public class LockCarAdapterImpl extends  AdapterParam implements LockCarAdapter{

	private static final Log _log = LogFactory.getLog(LockCarAdapterImpl.class);
	
	public LockCarAdapterImpl(String url){
		this.setAdapterUrl(url);
	}
	
	public LockCarAdapterImpl(){
    }
	
	@Override
	public void createLockCarOrder(AdLockCarOrder apLockCarOrder) throws AdapterException {
		String url = BSGConfig.getAdapterUrl();
		String startTime=DateUtils.formatDate(apLockCarOrder.getStartTime(), DateUtils.DATE_LONG_DATE);
		String endTime=DateUtils.formatDate(apLockCarOrder.getEndTime(), DateUtils.DATE_LONG_DATE);
		String orderId=apLockCarOrder.getOrderId();
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("clientID", apLockCarOrder.getParkCode());
		jsonObj.put("cardCode", "19");
		jsonObj.put("plateNo", apLockCarOrder.getCarNum());
		jsonObj.put("dataFlag", "2");//表示车牌
		jsonObj.put("flag", "1");
		jsonObj.put("startTime", startTime);
		jsonObj.put("endTime", endTime);
		jsonObj.put("orderID", apLockCarOrder.getOrderId());
		Map<String, String> params = new HashMap<String, String>();
		params.put("UserID", "110");
		params.put("RequestFun", ConstantFunction.Parking_LockCar);
		params.put("RequestData", jsonObj.toJSONString());
		params.put("Sign", "ddd");
		try {
			_log.info("adapter request:"+params);
			String result = HttpUtil.doPost(url, params);
			_log.info("adapter response:"+result);
			java.io.Reader in = new StringReader(result);
			Document doc=doc = (new SAXBuilder()).build(in);

			String temp = doc.getRootElement().getValue();
			in.close();
			ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
			String errorCode = repmsg.getErrorCode();
			if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
				throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
						ExceptionMessage.createExceptionMessage(
								AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
								ConstantFunction.Parking_LockCar + " adpater处理出错: "+repmsg.getErrorMsg()+",orderid="+orderId));
			}
		} catch (AdapterException e) {
			throw e;
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void unLockCar(AdLockCarOrder apLockCarOrder) throws AdapterException {
	    String url = BSGConfig.getAdapterUrl();
		String startTime=DateUtils.formatDate(apLockCarOrder.getStartTime(), DateUtils.DATE_LONG_DATE);
		String endTime=DateUtils.formatDate(apLockCarOrder.getEndTime(), DateUtils.DATE_LONG_DATE);
		String orderId=apLockCarOrder.getOrderId();
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("clientID", apLockCarOrder.getParkCode());
		jsonObj.put("cardCode", "19");
		jsonObj.put("plateNo", "");
		jsonObj.put("dataFlag", "");//表示车牌
		jsonObj.put("flag", "2");
		jsonObj.put("startTime", "");
		jsonObj.put("endTime", "");
		jsonObj.put("orderID", orderId);
		Map<String, String> params = new HashMap<String, String>();
		params.put("UserID", "110");
		params.put("RequestFun", ConstantFunction.Parking_LockCar);
		params.put("RequestData", jsonObj.toJSONString());
		params.put("Sign", "ddd");
		_log.info(jsonObj.toJSONString());
		try {
			_log.info("adapter request:"+params);
			String result = HttpUtil.doPost(url, params);
			_log.info("adapter response:"+result);
			java.io.Reader in = new StringReader(result);
			Document doc=doc = (new SAXBuilder()).build(in);

			String temp = doc.getRootElement().getValue();
			in.close();
			ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
			String errorCode = repmsg.getErrorCode();
			if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
				throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
						ExceptionMessage.createExceptionMessage(
								AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
								ConstantFunction.Parking_LockCar + " adpater处理出错: "+repmsg.getErrorMsg()+",orderid="+orderId));
			}
		} catch (AdapterException e) {
			throw e;
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}
